// pages/staff/newmyyeji.js
const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require('../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index:0,
        showernon:false,
        datestart:'开始时间',
        dateend:'结束时间',
        area: [{ 'area_index': 0, 'area_name': '全部' }, { 'area_index': 1, 'area_name': '未确认' }, { 'area_index': 2, 'area_name': '已确认' }],
        area_index:0,
        lists: [],
        amount:0,
        code:'',
        msg:'',
        consultant_id:'',
        page:0,
        everytitle:'全部',
        amounttitle:'',
    },


    copetypa1(e) {
        var copy = e.currentTarget.dataset.phone;
        wx.setClipboardData({
            data: copy,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: 'none',
                    duration: 1500
                })
            },
            fail: function (res) {
                wx.showToast({
                    title: '复制失败',
                    icon: 'none',
                    duration: 1500
                })
            },
        })
    },


    // 点击复制
    copetypa(e) {
        var copy = e.currentTarget.dataset.order;
        wx.setClipboardData({
            data: copy,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: 'none',
                    duration: 1500
                })
            },
            fail: function (res) {
                wx.showToast({
                    title: '复制失败',
                    icon: 'none',
                    duration: 1500
                })
            },
        })
    },

    
    // 点击确认
    surechoosechong(){
        var that = this;
        var datestart = that.data.datestart;
        var dateend = that.data.dateend;
        var area_index = that.data.area_index;
        var area = that.data.area;
        if (datestart =='开始时间'){
            var title = area[area_index].area_name;
            var amounttitle = '';
            that.tit(title, amounttitle)
        }else{
            if (dateend =='结束时间'){
                // var title = datestart+'~至今';
                // var amounttitle = area[area_index].area_name+':'
                wx.showToast({
                    title: '请选择结束时间',
                    icon: 'none',
                    duration: 1500
                })
            }else{
                var title = datestart + '~' + dateend;
                var amounttitle = area[area_index].area_name + ':';
                that.tit(title, amounttitle)
            } 
        }
        
    },

    tit(title, amounttitle){
        var that = this;
        that.setData({
            showernon: false,
            everytitle: title,
            lists: [],
            amount: 0,
            msg: '',
            page: 0,
            amounttitle: amounttitle,
        })
        that.yejiLoadMore();
    },
    

    
    // 点击重置
    choosechong(){
       var that = this;
       that.setData({
           area_index:0,
           showernon: false,
           datestart: '开始时间',
           dateend: '结束时间',
           lists: [],
           amount: 0,
           msg: '',
           page: 0,
           amounttitle:'',
           everytitle:'全部',
       })
        that.yejiLoadMore();
    },
    
    // 选择业绩
    chooseyeji(e){
        var that = this;
        var index = e.target.dataset.index;
        that.setData({
            area_index: index
        })
    },

    
    // 点击空白
    showernon(){
        var that = this;
        that.setData({
            showernon: false
        })
    },
    

    // 点击今日业绩
    yejinon(){
        var that = this;
        that.setData({
            showernon: true,
        })
    },

    // 切换title
    titleclick(e){
        var that = this;
        var index = e.target.dataset.index;
        that.setData({
            index:index,
            area_index: 0,
            showernon: false,
            datestart: '开始时间',
            dateend: '结束时间',
            lists: [],
            amount: 0,
            msg: '',
            page: 0,
            everytitle: '全部',
            amounttitle: '',
        })
        that.yejiLoadMore();
    },

    //  选择起始时间
    bindDateChangestart: function (e) {
        this.setData({
            datestart: e.detail.value
        })
    },

    // 结束时间
    bindDateChangeend: function (e) {
        // console.log(this.data.datestart == '起始时间');
        if (this.data.datestart == '开始时间') {
            wx.showToast({
                title: '请先选择开始时间',
                icon: 'none',
                duration: 1500
            })
        } else {
            this.setData({
                dateend: e.detail.value
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var consultant_id = app.globalData.admin_id;
        that.setData({
            consultant_id:consultant_id,
        })
        that.yejiLoadMore();
    },




    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onReachBottom: function () {
        var that = this;
        var page = Number(that.data.page)+1;
        that.setData({
            page:page,
        })
        that.yejiLoadMore();
    },



    yejiLoadMore: function () {
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        let that = this;
        var consultant_id = that.data.consultant_id;
        var datestart = that.data.datestart;
        var dateend = that.data.dateend;
        var area_index = that.data.area_index;
        var page = that.data.page;
        if (datestart=='开始时间'){
            var data = {
                consultant_id: consultant_id,
                ucp_status: area_index,
                page: page,
            }
        }else{
            if (dateend == '结束时间') {
                var data = {
                    consultant_id: consultant_id,
                    ucp_status: area_index,
                    start_date: datestart,
                    page: page
                }
            }else{
                var data = {
                    consultant_id: consultant_id,
                    ucp_status: area_index,
                    start_date: datestart,
                    end_date: dateend,
                    page: page
                }
            }
        }
        

        // var index = that.data.index;
        // if(Number(index)==0){
        //     var url = CONFIG.API_HOST + 'crm/static/getCoursePay';
        // }else{
        //     var url = CONFIG.API_HOST + 'crm/static/getCoursePayWk';
        // }
        
        if (Number(app.globalData.identity) == 3) {
            var url = 'crmapi/Learn/allUserCoursePay'
        } else {
            var url = 'crmapi/user/allUserCoursePay'
        }
        share.wxRequest(url,data,function(res){
            // console.log(res);
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var newslist = data.lists;
                if(Number(page)==0){
                    var lists = newslist;
                }else{
                    var lists = that.data.lists.concat(newslist);
                }
                if(Number(lists.length)==0){
                    var msg = '~加油~'
                }else{
                    var msg = res.data.msg
                }
                that.setData({
                    code: res.data.errcode,
                    msg: msg,
                    lists: lists,
                    amount: data.countAmount,
                })
            } else {
                that.setData({
                    code: res.data.errcode,
                    msg: res.data.msg,
                    lists: that.data.lists,
                    amount:0,
                })
            }
            wx.hideLoading();
        })
    },
    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },




    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

})